package controler;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;

import settings.Settings;
import view.BaseHybridView;

import graphics.engine.RenderEngine;

public class KeyboardControler implements KeyListener {
	private RenderEngine renderEngine;
	
	public KeyboardControler(RenderEngine renderEngine)
	{
		this.renderEngine = renderEngine;
	}
	@Override
	public void keyPressed(KeyEvent event) 
	{
		if(event.isControlDown() && event.getKeyCode() == KeyEvent.VK_A)
		{
			this.renderEngine.selectAllRooms();
		}	
		if(event.isControlDown() && event.getKeyCode() == KeyEvent.VK_D)
		{
			this.renderEngine.getSelectionManager().clearRoomSelection();
		}
		if(event.isControlDown() && event.getKeyCode() == KeyEvent.VK_S)
		{
			this.renderEngine.getModel().saveVisibleTrajectory();
		}
		if(event.getKeyCode() == KeyEvent.VK_R)
		{
			this.renderEngine.getCamera().Reset();
		}			
		if(event.getKeyCode() == KeyEvent.VK_C)
		{
			for(Iterator<BaseHybridView> it = this.renderEngine.getSelectionManager().getSelectedNodes().iterator(); it.hasNext(); )
			{
				BaseHybridView node = it.next();
				node.setNodeShape(true);
			}
		}			
		if(event.getKeyCode() == KeyEvent.VK_E)
		{
			for(Iterator<BaseHybridView> it = this.renderEngine.getSelectionManager().getSelectedNodes().iterator(); it.hasNext(); )
			{
				BaseHybridView node = it.next();
				node.setNodeShape(false);
			}
		}			
		if(event.getKeyCode() == KeyEvent.VK_I)
		{
			for(Iterator<BaseHybridView> it = this.renderEngine.getSelectionManager().getSelectedNodes().iterator(); it.hasNext(); )
			{
				BaseHybridView node = it.next();
				node.setNodeShape(!node.isNodeShape());
			}
		}		
		if(event.getKeyCode() == KeyEvent.VK_F1)
		{
			Settings.DrawSubsets = !Settings.DrawSubsets;
		}	
		if(event.getKeyCode() == KeyEvent.VK_F2)
		{
			Settings.DrawRooms = !Settings.DrawRooms;
		}				
		if(event.getKeyCode() == KeyEvent.VK_F3)
		{
			Settings.DrawDoors = !Settings.DrawDoors;
		}				
		if(event.getKeyCode() == KeyEvent.VK_F4)
		{
			Settings.DrawAjacencies = !Settings.DrawAjacencies;
		}				
		if(event.getKeyCode() == KeyEvent.VK_F5)
		{
			Settings.DrawSelection = !Settings.DrawSelection;
		}				
		if(event.getKeyCode() == KeyEvent.VK_F6)
		{
			Settings.DrawTrajetories = !Settings.DrawTrajetories;
		}			
		if(event.getKeyCode() == KeyEvent.VK_F7)
		{
			Settings.HideUnnecessaryObjects = !Settings.HideUnnecessaryObjects;
		}	
		if(event.getKeyCode() == KeyEvent.VK_F8)
		{
			Settings.AutoFadeTraj = !Settings.AutoFadeTraj;
		}	
		
		if(event.getKeyCode() == KeyEvent.VK_O)
		{
			Settings.BezierWeightFactor = Settings.BezierWeightFactor + 0.1;
			this.renderEngine.getView().trajectoryesHaveChanged(true);
		}		
		if(event.getKeyCode() == KeyEvent.VK_L)
		{
			Settings.BezierWeightFactor = Settings.BezierWeightFactor - 0.1;
			this.renderEngine.getView().trajectoryesHaveChanged(true);
		}	
		if(event.getKeyCode() == KeyEvent.VK_P)
		{
			Settings.DebugBezier = !Settings.DebugBezier;
		}	
				
		if(event.getKeyCode() == KeyEvent.VK_F12)
		{
			if(Settings.isStraightDoorLinks())
			{
				Settings.setPolylineLinks(true);
			}
			else if(Settings.isPolylineLinks())
			{
				Settings.setBezierLinks(true);
			}
			else
			{
				Settings.setStraightDoorLinks(true);
			}
			this.renderEngine.getView().trajectoryesHaveChanged(true);
		}
		if(event.getKeyCode() == KeyEvent.VK_V)
		{
			for(Iterator<BaseHybridView> it = this.renderEngine.getSelectionManager().getSelectedNodes().iterator(); it.hasNext(); )
			{
				BaseHybridView node = it.next();
				node.setNodeDetailLevel(2);
			}
		}	
	}

	@Override
	public void keyReleased(KeyEvent event) 
	{
		//this.isCtrlPressed = (event.getKeyCode() == KeyEvent.VK_CONTROL);
	}

	@Override
	public void keyTyped(KeyEvent event) 
	{

	}
}
